package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class FactoryVersion extends AxdrType {
    private ByteBuffer dataCode = null;
    private VISIBLESTRING4 fcode = null;
    private VISIBLESTRING4 sver = null;
    private VISIBLESTRING6 sverDate = null;
    private VISIBLESTRING4 hver = null;
    private VISIBLESTRING6 hverDate = null;
    private VISIBLESTRING8 fextend = null;

    public FactoryVersion() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = ByteBuffer.wrap(dataCode);
    }

    public void setAll(VISIBLESTRING4 fcode, VISIBLESTRING4 sver, VISIBLESTRING6 sverDate, VISIBLESTRING4 hver, VISIBLESTRING6 hverDate, VISIBLESTRING8 fextend) {
        this.fcode = fcode;
        this.sver = sver;
        this.sverDate = sverDate;
        this.hver = hver;
        this.hverDate = hverDate;
        this.fextend = fextend;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.fcode = new VISIBLESTRING4();
        codeLength += this.fcode.decode(input);
        this.sver = new VISIBLESTRING4();
        codeLength += this.sver.decode(input);
        this.sverDate = new VISIBLESTRING6();
        codeLength += this.sverDate.decode(input);
        this.hver = new VISIBLESTRING4();
        codeLength += this.hver.decode(input);
        this.hverDate = new VISIBLESTRING6();
        codeLength += this.hverDate.decode(input);
        this.fextend = new VISIBLESTRING8();
        codeLength += this.fextend.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            byte[] data = this.dataCode.array();
            codeLength = data.length;
            for (int i = data.length - 1; i >= 0; i--) {
                output.write(data[i]);
            }
        } else {
            if (this.fcode != null && this.sver != null && this.sverDate != null && this.hver != null && this.hverDate != null && this.fextend != null) {
                codeLength += this.fextend.encode(output);
                codeLength += this.hverDate.encode(output);
                codeLength += this.hver.encode(output);
                codeLength += this.sverDate.encode(output);
                codeLength += this.sver.encode(output);
                codeLength += this.fcode.encode(output);
            } else {
                throw new RuntimeException("fextend || hverDate || hver || sverDate || sver || fcode is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.fcode != null && this.sver != null && this.sverDate != null && this.hver != null && this.hverDate != null && this.fextend != null) {
            return "sequence: {" + "fcode: " + this.fcode + ", sver: " + this.sver + ", sverdate: " + this.sverDate + ", hver: " + this.hver + ", hverdate: " + this.hverDate + ", fextend: " + this.fextend + "}";
        } else {
            return "fextend || hverDate || hver || sverDate || sver || fcode is null";
        }
    }
}


